//
//  GAOStatusesRepostView.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

//转发微博视图
class GAOStatusesRepostView: UIView {
    //记录当前视图的底部约束
    var repostViewBottom: Constraint?
    
    var statusesViewModel: GAOStatusesViewModel? {
        didSet {
            //卸载重复修改的的约束
            repostViewBottom?.uninstall()
            
//            contentLabel.text = statusesViewModel?.repostContent
            //设置富文本
            contentLabel.attributedText = statusesViewModel?.retweetAttributedString
            
            if let pic_urls = statusesViewModel?.statuses?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                //显示配图
                pictureView.hidden = false
                //设置配图信息
                pictureView.pic_urls = pic_urls
                //更新UI
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.repostViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
            }else {
                //隐藏配图
                pictureView.hidden = true
                //更新UI
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.repostViewBottom = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
            
        }
    }
    
    //转发的文字内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        label.numberOfLines = 0
        
        return label
    }()
    //转发内容的配图视图
    private lazy var pictureView: GAOStatusesPictureView = {
        let view = GAOStatusesPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        
        //当前视图的约束
        self.snp_makeConstraints { (make) -> Void in
           self.repostViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }
    
}
